package com.teacher.game.framework.util;

import android.graphics.Color;

/**
 * 调试HUD - 在游戏界面显示性能信息
 * 用于实时监控和测试优化效果
 */
public class DebugHUD {
    
    private static DebugHUD instance;
    private boolean enabled = false;
    
    // 性能监控器引用
    private PerformanceMonitor performanceMonitor;
    private MemoryLeakDetector memoryLeakDetector;
    private BitmapCache bitmapCache;
    
    // 显示位置
    private static final int HUD_X = 10;
    private static final int HUD_Y = 10;
    private static final int LINE_HEIGHT = 20;
    
    /**
     * 私有构造函数
     */
    private DebugHUD() {
        performanceMonitor = PerformanceMonitor.getInstance();
        memoryLeakDetector = MemoryLeakDetector.getInstance();
        bitmapCache = BitmapCache.getInstance();
    }
    
    /**
     * 获取单例实例
     * @return DebugHUD实例
     */
    public static DebugHUD getInstance() {
        if (instance == null) {
            instance = new DebugHUD();
        }
        return instance;
    }
    
    /**
     * 启用/禁用调试HUD
     * @param enabled 是否启用
     */
    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }
    
    /**
     * 检查是否启用
     * @return 是否启用
     */
    public boolean isEnabled() {
        return enabled;
    }
    
    /**
     * 渲染调试信息
     * @param painter 绘制工具
     */
    public void render(Painter painter) {
        if (!enabled) return;
        
        // 设置背景
        painter.setColor(Color.argb(128, 0, 0, 0)); // 半透明黑色背景
        painter.fillRect(HUD_X - 5, HUD_Y - 5, 300, 180);
        
        // 设置文字颜色
        painter.setColor(Color.WHITE);
        
        int currentY = HUD_Y + 10;
        
        // 标题
        painter.drawString("=== 性能监控 ===", HUD_X, currentY);
        currentY += LINE_HEIGHT;
        
        // FPS信息
        painter.drawString(String.format("FPS: %.0f", performanceMonitor.getCurrentFPS()), 
                          HUD_X, currentY);
        currentY += LINE_HEIGHT;
        
        // 渲染时间
        painter.drawString(String.format("渲染: %.1fms", performanceMonitor.getAverageRenderTime()), 
                          HUD_X, currentY);
        currentY += LINE_HEIGHT;
        
        // 内存使用率
        painter.drawString(String.format("内存: %.0f%%", performanceMonitor.getCurrentMemoryUsage() * 100), 
                          HUD_X, currentY);
        currentY += LINE_HEIGHT;
        
        // Bitmap缓存信息
        painter.drawString(String.format("缓存: %d/%dKB", bitmapCache.getCacheSize(), bitmapCache.getMaxCacheSize()), 
                          HUD_X, currentY);
        currentY += LINE_HEIGHT;
        
        // 缓存命中率
        painter.drawString(String.format("命中率: %d%%", bitmapCache.getCacheHitRate()), 
                          HUD_X, currentY);
        currentY += LINE_HEIGHT;
        
        // 内存泄漏信息
        painter.drawString(memoryLeakDetector.getMemoryStats(), HUD_X, currentY);
        currentY += LINE_HEIGHT;
        
        // 性能警告指示器
        if (performanceMonitor.hasPerformanceIssues()) {
            painter.setColor(Color.RED);
            painter.drawString("⚠️ 性能警告", HUD_X + 200, HUD_Y + 10);
        }
    }
    
    /**
     * 切换调试模式
     */
    public void toggle() {
        enabled = !enabled;
    }
}